package org.example.common.security.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.example.common.core.enums.ResultCode;
import org.example.common.core.model.Result;
import org.example.common.security.exception.ServiceException;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Collection;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author : [Pashm]
 * @version : [v1.0]
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 请求方式不支持
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result<?> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request){
        String requestURI = request.getRequestURI();
        log.error("请求地址 '{}', 不支持' {} '请求", requestURI, e.getMethod());
        return Result.fail(ResultCode.ERROR);
    }
    /**
     * 自定义运行时异常
     */
    @ExceptionHandler(ServletException.class)
    public Result<?> handleServiceException(ServiceException e, HttpServletRequest request){
        String requestURI = request.getRequestURI();
        ResultCode resultCode = e.getResultCode();
        log.error("请求地址'{}', 发生业务异常:{}", requestURI, resultCode.getMsg(), e);
        return Result.fail(resultCode);

    }

    /**
     * 拦截运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public Result<?> handleRuntimeException(RuntimeException e, HttpServletRequest request){
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}', 发生异常", requestURI,e);
        return Result.fail(ResultCode.ERROR);

    }

    /**
     *拦截参数异常
     */
    @ExceptionHandler(BindException.class)
    public Result<Void> handleBindException(BindException e) {
         log.error(e.getMessage());
         String message = join(e.getAllErrors(),
                DefaultMessageSourceResolvable::getDefaultMessage, ", ");
         return Result.fail(ResultCode.FAILED_PARAMS_VALIDATE.getCode(), message);
         }

    private <E> String join(Collection<E> collection, Function<E, String>
            function, CharSequence delimiter) {
         if (CollUtil.isEmpty(collection)) {
             return StrUtil.EMPTY;
             }
         return collection.stream().map(function).filter(Objects::nonNull).collect(Collectors.joining(delimiter));
         }
    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public Result<?> handleException(Exception e, HttpServletRequest  request){
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生异常", requestURI, e);
        return Result.fail(ResultCode.ERROR);
    }

}
